﻿//google.load("maps", "2");

// make a json request to get the map data from the Map action
$(function() {
    if (google.maps.BrowserIsCompatible()) {
        initialize();
        showAddressOnPageLoad();
    }
});


var map;
var geocoder;
var address;

function initialize() {
    map = new GMap2(document.getElementById("map_canvas"));
    //map.setCenter(new GLatLng(40.730885, -73.997383), 15);
    map.setUIToDefault();
    GEvent.addListener(map, "click", getAddress);
    geocoder = new GClientGeocoder();
}

function getAddress(overlay, latlng) {
    if (latlng != null) {
        address = latlng;
        geocoder.getLocations(latlng, showAddress);
    }
}

function showAddress(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
        '<b>orig latlng:</b>' + response.name + '<br/>' +
        '<b>latlng:</b>' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Status Code:</b>' + response.Status.code + '<br>' +
        '<b>Status Request:</b>' + response.Status.request + '<br>' +
        '<b>Address:</b>' + place.address + '<br>' +
        '<b>Accuracy:</b>' + place.AddressDetails.Accuracy + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);


        document.getElementById("Location_LatLng_Latitude").value = place.Point.coordinates[1];
        document.getElementById("Location_LatLng_Longitude").value = place.Point.coordinates[0];

    }
}

function showAddressWithSelectionChange() {

    if (document.getElementById("CountryList").selectedIndex != 0) {

        address = document.getElementById("CountryList").options[document.getElementById("CountryList").selectedIndex].text;
        address = document.getElementById("Location_Place").value + ", " + address;

        showThisAddress(address);
        
    }
}

function showAddressOnPageLoad() {

    
    if (document.getElementById("Location_LatLng_Latitude").value != 0) {

        point = new GLatLng(document.getElementById("Location_LatLng_Latitude").value,
                            document.getElementById("Location_LatLng_Longitude").value);
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(
                        '<b>lattitude:</b>' + center.lat() + '<br>' +
                        '<b>longitude:</b>' + center.lng() + '<br>');

    }
}

function showThisAddress(address) {

    if (geocoder) {
        geocoder.getLatLng(
              address,
              function(point) {
                  if (!point) {
                      alert(address + " not found");
                  } else {
                      map.setCenter(point, 13);
                      var marker = new GMarker(point);
                      var center = map.getCenter();
                      
                      
                      map.addOverlay(marker);
                      marker.openInfoWindowHtml(
                        '<b>lattitude:</b>' + center.lat() + '<br>' +
                        '<b>longitude:</b>' + center.lng() + '<br>' );

                      document.getElementById("Location_LatLng_Latitude").value = center.lat();
                      document.getElementById("Location_LatLng_Longitude").value = center.lng();
                  }
              }
            );
    }

}
 